/** 
 * GraphicsUtils.java
 * @author Lex
 * 2007-8-19 ����02:28:18
 */
package org.lex.awt;

import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Point;

import javax.swing.JComponent;

/**
 * 
 */
public class GraphicsUtils {
	private GraphicsUtils() {
	}

	/**
	 * Designate the place where the string will be painted. This implementation
	 * places it at the center of the JComponent (in both x and y).
	 */
	public static Point getStringPlacement(JComponent c, String str) {
		FontMetrics fm = c.getFontMetrics(c.getFont());
		int strWidth = fm.stringWidth(str);
		Insets insets = c.getInsets();
		int actualWidth = c.getWidth() - insets.left - insets.right;
		int actualHeight = c.getHeight() - insets.top - insets.bottom;
		Point p = new Point(insets.left, insets.top);
		p.x += (actualWidth - strWidth) / 2;
		p.y += (actualHeight + fm.getAscent() - fm.getLeading() - fm.getDescent()) / 2;
		return p;
	}

	/**
	 * �ڸ߶�Ϊheight�Ŀռ��У��� str �����м�(��ֱ����)
	 * 
	 * @param fm
	 * @param height
	 * @return
	 */
	public static int getVerticalPlacement(FontMetrics fm, int height) {
		return (height + fm.getAscent() - fm.getLeading() - fm.getDescent()) / 2;
	}
}
